package com.kernowsoft.wavefx;

import static java.lang.Math.cos;
import static java.lang.Math.min;
import static java.lang.Math.sin;
import static java.lang.Math.toRadians;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class WindBarb extends View {

	private static final String TAG = WindBarb.class.getSimpleName();
	
	private float mWindSpeed;
	private float mWindDir;
	
	private float mBarbRatio = 0.3f;	// ratio of flag length to barb length
	private float mBarbAngle = (float)toRadians(60.0f);	// angle of barb from shaft
	private float mBarbSpacing = 0.15f;	// spacing of barbs, relative to shaft length
	
	
	// painters:
	private Paint mBarbPainter;
	
	
	public WindBarb(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private final void init() {
		mBarbPainter = new Paint();
		mBarbPainter.setStrokeCap(Cap.ROUND);
		mBarbPainter.setStrokeWidth(2);
		mBarbPainter.setStyle(Style.STROKE);
		mBarbPainter.setColor(Color.BLACK);
		mBarbPainter.setAntiAlias(true);
		
		// initialiser values for tools view:
		if(isInEditMode()) {
			mWindSpeed = 37;
			mWindDir = 45;
		}
	}
	
	public void setDirection(float dir) {
		this.mWindDir = dir;
	}
	
	public void setSpeed_kts(float speed) {
		this.mWindSpeed = speed;
	}
	
	public void setSpeed_mps(float speed) {
		this.mWindSpeed = speed / 1.414f;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		
		float x1, y1, x2, y2;
		
		float w = getWidth();
		float h = getHeight();

		double cosang = cos(mBarbAngle);
		double sinang = sin(mBarbAngle);
		
		// get centre of view:
		float xc = w / 2f;
		float yc = h / 2f;
		
		//Log.d(TAG, String.format("Width/Height/Centre: %f, %f, [%f,%f]", w, h, xc, yc));
		// determine barb length (default is to fill view, along minimum length):
		float totalLen = min(w, h);
		float barbLen = totalLen * mBarbRatio;
		// shaft length is total length, minus the overhanging barb length:
		float shaftLen = totalLen - (float)(cosang * barbLen);
		
		
				
		
		// Use canvas rotation to deal with wind barb direction.
		// All drawing can now be done on a barb that is horizontal, i.e.:
		/*
		                                        /
		                                       /
		                                /     /
		                               /     /
		          ----------------------------
		*/
		
		canvas.save();
		// arrow is draw as above when rotation is zero, canvas rotates
		// clockwise, so rotation angle is (nautical) windDir - 90
		float cdir = (float)(mWindDir - 90) % 360.0f;
		canvas.rotate(cdir, xc, yc);
		
		// draw shaft
		float shaftStart = xc - 0.5f * totalLen; 
		float shaftEnd = shaftStart + shaftLen;
		canvas.drawLine(shaftStart, yc, shaftEnd, yc, mBarbPainter);
		
		// draw barbs, starting at end of shaft:
		x1 = shaftEnd;
		float spd = mWindSpeed;
		
		// 50+ knots
		while((int)((spd + 5) / 50) > 0) {
			// TODO: draw flag
			spd -= 50;
//			Log.d(TAG, "Added 50kt barb");
			
			// straight line
			x2 = x1;
			y1 = yc;
			y2 = y1 + (float)(sinang * barbLen);
			canvas.drawLine(x1, y1, x2, y2, mBarbPainter);
			
			// barb joining line:
			x1 -= (float)(cosang * barbLen);
			x2 = x1 + (float)(cosang * barbLen);
			y1 = yc;
			y2 = y1 + (float)(sinang * barbLen);
			canvas.drawLine(x1, y1, x2, y2, mBarbPainter);			
			
			x1 -= mBarbSpacing * shaftLen;
		}
		
		// 10 knots
		while((int)((spd + 2.5) / 10) > 0) {
			// draw full length barb
			spd -= 10;
//			Log.d(TAG, "Added 10kt barb");
			x2 = x1 + (float)(cosang * barbLen);
			y1 = yc;
			y2 = y1 + (float)(sinang * barbLen);
			canvas.drawLine(x1, y1, x2, y2, mBarbPainter);
			
			x1 -= mBarbSpacing * shaftLen;
		}
		
		// 5 knots
		while((int)((spd + 2.5) / 5) > 0) {
			// draw full length barb
			spd -= 5;
//			Log.d(TAG, "Added 5kt barb");
			x2 = x1 + (float)(cosang * 0.5f * barbLen);
			y1 = yc;
			y2 = y1 + (float)(sinang * 0.5f * barbLen);
			canvas.drawLine(x1, y1, x2, y2, mBarbPainter);
			
			x1 -= mBarbSpacing * shaftLen;
		}
		
		
		// restore canvas rotation:
		canvas.restore();
	}
}
